﻿using System;
using System.Windows;

using Game.Common;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;

namespace Game.Surface
{
    public class MovePathAnamation
    {
        readonly Vehicle _Item;
        readonly GameCanvas _Canvas;
        readonly SurfaceControl _SurfaceControl;
        readonly GamePath _GamePath;
        readonly IActionCallback _ActionCallback;
        readonly GameTurn _GameTurn;
        readonly Vehicle _Vehicle;
        
        int _Index;
        bool _IsComplete;
        MoveVehicleAnamation _Anamation;

        public MovePathAnamation(
            SurfaceControl control,
            GameCanvas canvas,
            MoveEventArgs moveEventArgs,
            GameTurn turn)
        {
            _SurfaceControl = control;
            _Canvas = canvas;
            _GamePath = moveEventArgs.Path;
            _Item = moveEventArgs.Item;
            _ActionCallback = moveEventArgs.Callback;
            _ActionCallback.Start(turn);
            _GameTurn = turn;
            _Vehicle = _Item as Vehicle;
        }
        
        public bool IsComplete
        {
            get { return _IsComplete; }
        }
        
        public void Start()
        {
            LaunchAnamation();
        }
        
        void LaunchAnamation()
        {
            if (_Index < _GamePath.Points.Count - 1)
            {
                var p1 = _GamePath.Points[_Index];
                var start = new Point(p1.X, p1.Y);
                var startField = _SurfaceControl.Fields[start].MapField;

                var p2 = _GamePath.Points[_Index + 1];
                var end = new Point(p2.X, p2.Y);
                var endField = _SurfaceControl.Fields[end].MapField;

                _Anamation = new MoveVehicleAnamation(
                    _SurfaceControl, _Canvas, startField, endField, _Item);
                _Anamation.Complete += OnComplete;

                endField.MovingX().MovingItem = _Vehicle;
                startField.VehicleX().Vehicle = null;
                startField.MovingX().MovingItem = null;

                _Index++;
                _Anamation.Start();
            }
            else
            {
                var p1 = _GamePath.Points[_Index];
                var start = new Point(p1.X, p1.Y);
                var startField = _SurfaceControl.Fields[start].MapField;

                var p2 = _GamePath.Points[_Index];
                var end = new Point(p2.X, p2.Y);
                var endField = _SurfaceControl.Fields[end].MapField;

                endField.VehicleX().Vehicle = _Item;
                startField.MovingX().MovingItem = null;
                endField.MovingX().MovingItem = null;

                _IsComplete = true;
                _ActionCallback.Done(_GameTurn, true);
            }
        }
        
        void OnComplete(object sender, EventArgs e)
        {
            _Anamation.Complete -= OnComplete;
            LaunchAnamation();
        }
        
    }
}
